package com.j180.service.petOwner.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.j180.bean.PageBean;
import com.j180.bean.PetOwner;
import com.j180.bean.ResultBean;
import com.j180.dao.petOwner.IPetOwnerDao;
import com.j180.service.petOwner.IPetOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PetOwnerServiceImpl implements IPetOwnerService {

    @Autowired
    private IPetOwnerDao petOwnerDao;

    @Override
    public PageBean<PetOwner> getAllPetOwners(Map<String, Object> map) {

        PageBean<PetOwner> pageBean = new PageBean<PetOwner>();
        //分页条件
        int currentPage = Integer.parseInt(String.valueOf(map.get("currentPage")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));

        PageHelper.startPage(currentPage,pageSize);
        //查询数据
        List<PetOwner> list = petOwnerDao.getAllPetOwners();
        PageInfo<PetOwner> pageInfo = new PageInfo<PetOwner>(list);
        //封装自己的分页对象
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTolNum((int) pageInfo.getTotal());
        pageBean.setTolPage(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());

        return pageBean;
    }

    @Override
    public PetOwner getPetOwnerById(int id) {
        PetOwner petOwner = petOwnerDao.getPetOwnerById(id);
        return petOwner;
    }

    @Override
    public int addPetOwner(PetOwner petOwner) {
        int count = petOwnerDao.insertPetOwner(petOwner);
        return count;
    }

    @Override
    public int deletePetOwnerById(int id) {
        int count = petOwnerDao.deletePetOwnerById(id);
        return count;
    }

    @Override
    public int updatePetOwnerInfo(PetOwner petOwner) {
        int count = petOwnerDao.updatePetOwnerInfo(petOwner);
        return count;
    }
}
